package CareerUp.CamelCase;

import java.util.ArrayList;

/**
 * 
 * Write a method that takes a camelCase string as a parameter and returns
 * underscore_case as output. Assume that input can be null or empty. If
 * CamilCase parametar starts with a capital letter turn it into lower case
 * without puting underscore before it. How do you test this method?
 * 
 * @author Xi
 * 
 */

public class CamelCase {

	public String input;

	/**
	 * hanlde input string here- 1. if input string is null, set it to empty
	 * 2.if first letter is capital, set it to lower case
	 * 
	 * @param string
	 */
	public void setInputStr(String string) {

		if (string == null || string == "")
			input = "";
		else {
			// convert upper case to lower case
			int index = (int) (string.toCharArray()[0]);

			if (index >= 62 && index <= 90)
				index += 32;

			input = ((char) index) + string.substring(1);
		}
	}

	public String camelCaseConvertion() {
		if (input == "")
			return "";
		else {

			ArrayList<String> underScoreCase = new ArrayList<String>();

			for (int i = 0; i < input.length(); i++) {
				int index = (int) ((char) input.toCharArray()[i]);
				if (index >= 62 && index <= 90) {
					index += 32;
					underScoreCase.add("_");
					String temp = "" + ((char) index);
					underScoreCase.add(temp);
				} else {
					String temp = "" + ((char) index);
					underScoreCase.add(temp);
				}
			}

			String res = "";
			for (int i = 0; i < underScoreCase.size(); i++)
				res += underScoreCase.get(i);

			return res;
		}
	}
}
